package spider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * QQ群列表，QQ群成员，QQ好友 需要替换cookie，gc参数和bkn参数
 */
public class QQgroupSpider {

  public static void groupList() throws IOException {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClients.custom()
        .setRetryHandler(new DefaultHttpRequestRetryHandler())
        .setConnectionManager(cm)
        .build();
    HttpPost req = new HttpPost("https://qun.qq.com/cgi-bin/qun_mgr/get_group_list");
    req.setHeader("cookie",
        "pgv_pvi=65608704; RK=V1px5Fn3S+; ptcz=0868f62fa1f4d509ebd6017172257334aac2f1807ffd56c84dbe1bde959fc812; pgv_si=s9413700608; uin=o1982277202; skey=@jDjWF8Y3w; _qpsvr_localtk=0.6168954616944899; p_uin=o1982277202; pt4_token=Kr-0sEaq9hNibGjKI*IoXpuEG2DFbewWyyaW06mRy5M_; p_skey=RaxvN5AR60aicx1BdBaZKGW0-nqfqxjfp1y2oWM4WZs_; traceid=9b78efcd0e");
    req.setHeader(
        "User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36");
    req.setHeader("Host", "qun.qq.com");
    req.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
    req.setHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
    req.setHeader("Referer", "https://qun.qq.com/member.html");
    req.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    req.setHeader("X-Requested-With", "XMLHttpRequest");
    req.setHeader("Connection", " keep-alive");
    req.setHeader("Pragma", "no-cache");
    req.setHeader("Cache-Control", "no-cache");

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("bkn", "1693867637"));
    req.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
    HttpResponse resp = httpClient.execute(req);
    String sb = EntityUtils.toString(resp.getEntity());
    System.out.println(topicSpider.unicodeStr2String(sb));
  }

  public static void groupMembers() throws IOException {
    /**
     * 循环获取成员
     */
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClients.custom()
        .setRetryHandler(new DefaultHttpRequestRetryHandler())
        .setConnectionManager(cm)
        .build();
    HttpPost req = new HttpPost("https://qun.qq.com/cgi-bin/qun_mgr/search_group_members");
    req.setHeader("cookie",
        "pgv_pvi=65608704; RK=V1px5Fn3S+; ptcz=0868f62fa1f4d509ebd6017172257334aac2f1807ffd56c84dbe1bde959fc812; pgv_si=s9413700608; uin=o1982277202; skey=@jDjWF8Y3w; _qpsvr_localtk=0.6168954616944899; p_uin=o1982277202; pt4_token=Kr-0sEaq9hNibGjKI*IoXpuEG2DFbewWyyaW06mRy5M_; p_skey=RaxvN5AR60aicx1BdBaZKGW0-nqfqxjfp1y2oWM4WZs_; traceid=9b78efcd0e");
    req.setHeader(
        "User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36");
    req.setHeader("Host", "qun.qq.com");
    req.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
    req.setHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
    req.setHeader("Referer", "https://qun.qq.com/member.html");
    req.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    req.setHeader("X-Requested-With", "XMLHttpRequest");
    req.setHeader("Connection", " keep-alive");
    req.setHeader("Pragma", "no-cache");
    req.setHeader("Cache-Control", "no-cache");

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("st", "0"));
    params.add(new BasicNameValuePair("end", "20"));
    params.add(new BasicNameValuePair("gc", "1104747487"));
    params.add(new BasicNameValuePair("bkn", "1693867637"));
    params.add(new BasicNameValuePair("sort", "0"));
    req.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
    HttpResponse resp = httpClient.execute(req);
    String sb = EntityUtils.toString(resp.getEntity());
    System.out.println(topicSpider.unicodeStr2String(sb));
//        if (sb.length() < 25) break;
  }

  public static void friendList() throws IOException {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClients.custom()
        .setRetryHandler(new DefaultHttpRequestRetryHandler())
        .setConnectionManager(cm)
        .build();
    HttpPost req = new HttpPost("https://qun.qq.com/cgi-bin/qun_mgr/get_friend_list");
    req.setHeader("cookie",
        "pgv_pvi=65608704; RK=V1px5Fn3S+; ptcz=0868f62fa1f4d509ebd6017172257334aac2f1807ffd56c84dbe1bde959fc812; pgv_si=s9413700608; uin=o1982277202; skey=@jDjWF8Y3w; _qpsvr_localtk=0.6168954616944899; p_uin=o1982277202; pt4_token=Kr-0sEaq9hNibGjKI*IoXpuEG2DFbewWyyaW06mRy5M_; p_skey=RaxvN5AR60aicx1BdBaZKGW0-nqfqxjfp1y2oWM4WZs_; traceid=9b78efcd0e");
    req.setHeader(
        "User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36");
    req.setHeader("Host", "qun.qq.com");
    req.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
    req.setHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
    req.setHeader("Referer", "https://qun.qq.com/member.html");
    req.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    req.setHeader("X-Requested-With", "XMLHttpRequest");
    req.setHeader("Connection", " keep-alive");
    req.setHeader("Pragma", "no-cache");
    req.setHeader("Cache-Control", "no-cache");

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("bkn", "1693867637"));
    req.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
    HttpResponse resp = httpClient.execute(req);
    String sb = EntityUtils.toString(resp.getEntity());
    System.out.println(topicSpider.unicodeStr2String(sb));
  }

  public static void main(String[] args) throws IOException {
//        groupList();
//        groupMembers();
//        friendList();

  }
}
